package com.wf.day_0618.test1;

import com.wf.day_0618.test1.entity.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author mpk
 * @since 2024/6/18 19:13:43
 */
public class TestC {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

     /*   1. 定义一个Person类，包含属性name、age。

        2. 使用反射的方式创建一个实例、调用构造函数初始化name、age。使用反射方式调用setName方法对姓名进行设置，不使用setAge方法直接使用反射方式对age赋值。*/

        Class<?> aClass = Class.forName("com.wf.day_0618.test1.entity.Person");

        Constructor<?> constructor = aClass.getConstructor(String.class, int.class);


        Person person =(Person) constructor.newInstance("刻晴", 18);
        System.out.println("修改前：");
        System.out.println(person);

        Method setName = aClass.getMethod("setName", String.class);
        Method setAge = aClass.getMethod("setAge", int.class);


        setName.invoke(person, "凝光");
        setAge.invoke(person, 20);
        System.out.println("修改后：");
        System.out.println(person);
    }


}
